/*
 * Pipe.cpp
 *
 *  Created on: Mar 3, 2012
 *      Author: baneling
 */

#include "Pipe.h"

/*
 * This method reads on @inPipe argument
 * and returns a msg_t structure where:
 * msg_t->receiver contains:
 * 		- the receiver string name
 * 		- the value "none" if the read on pipe as returned -1
 * msg_t->sender contains:
 * 		- the sender string name
 * 		- the value "none" if the read on pipe as returned -1
 * msg_t->m_t contains:
 * 		- the string message readed on the pipe
 * 		- the value "none" if the read on pipe as returned -1
 * msg_t->x msg_t->y contains:
 * 		- the coordinates readed on the pipe
 * 		- the values -9999 if the read on pipe as returned -1
 * --> N.B. MAXIMUM 128 BYTES ARE READED <--
 */
msg_t Pipe::readOnPipe(int inPipe)
{
	msg_t mess;
	char *buff=NULL;
	buff=new char[128];
	int pos;

	if(read(inPipe,buff,128)!=-1)
	{
		string str=string(buff);
		//cout <<"read from pipe: "<< str <<endl;

		pos=str.find_first_of('&');
		mess.sender=str.substr(0,pos);
		str=str.substr(pos+1);

		pos=str.find_first_of('&');
		mess.receiver=str.substr(0,pos);
		str=str.substr(pos+1);

		pos=str.find_first_of('&');
		mess.m_t = Pipe::strToEnum(str.substr(0,pos));
		str=str.substr(pos+1);
		char *pEnd=NULL;
		mess.x = strtod (str.c_str(),&pEnd);
		mess.y = strtod (pEnd,NULL);
	}
	else
	{
		mess.sender="none";
		mess.receiver="none";
		mess.m_t=none;
		mess.x=-9999;
		mess.y=-9999;
	}
	delete buff;
	return mess;
}

/*
 * This method writes on the @outPipe argument
 * it returns the size_t bytes writed
 * the bytes are calculated by the sum of:
 * sizeof(@type)+sizeof("$")+sizeof(x)+sizeof(" ")+sizeof(y)
 * --> N.B. no checking on argument lenghts are done!!! <--
 * --> N.B. the reading method reads MAXIMUM 128 BYTES! <--
 */

size_t Pipe::writeOnPipe(int outPipe,string sender, string receiver,enum message_t type,double x, double y)
{
	size_t writedbytes;
	stringstream sendmsg;

	sendmsg<<sender<<"&"<<receiver<<"&"<<Pipe::enumToStr(type)<<"&"<<x<<" "<<y;
	//cout << sender << "-> sending to -> " << receiver << "( "<<sendmsg.str()<<" )"<<endl;
	writedbytes=sendmsg.str().length();
	write(outPipe,sendmsg.str().c_str(),writedbytes);
	return writedbytes;
}

string Pipe::enumToStr(enum message_t m)
{
	switch (m)
	{
	 case askState:
		 return "askState";
		 break;
	 case ansState:
		 return "ansState";
		 break;
	 case drFound:
		 return "drFound";
		 break;
	 case cabFound:
		 return "cabFound";
		 break;
	case none:
		return "none";
		break;
	case shift:
		return "shift";
		break;
	case okshift:
		return "okshift";
		break;
	case noshift:
		return "noshift";
		break;
	case awayFromDoc:
		return "awayFromDoc";
		break;
	case goToStop:
		return "goToStop";
	case sendFormationYaw:
		return "sendFormationYaw";
	case obstacles:
		return "obstacles";
	case drIsFree:
		return "drIsFree";
	case goFormation:
		return "goFormation";

	default:
		return "";
		break;
	}
	return "";
}

enum message_t Pipe::strToEnum(string s)
{
	if(s=="none")
	{
		return none;
	}
	if(s=="okshift")
	{
		return okshift;
	}
	if(s=="noshift")
	{
		return noshift;
	}
	if(s=="shift")
	{
		return shift;
	}

	if(s=="drFound")
	{
		return drFound;
	}
	if(s=="cabFound")
	{
		return cabFound;
	}

	if(s=="askState")
	{
		return askState;
	}

	if(s=="ansState")
	{
		return ansState;
	}
	if(s=="awayFromDoc")
	{
		return awayFromDoc;
	}
	if(s=="goToStop")
	{
		return goToStop;
	}
	if(s=="sendFormationYaw")
	{
		return sendFormationYaw;
	}
	if(s=="obstacles")
	{
		return obstacles;
	}
	if(s=="drIsFree")
	{
		return drIsFree;
	}
	if(s=="goFormation")
	{
		return goFormation;
	}
	return unreadable;
}
